package org.springblade.modules.system.wrapper;

import org.springblade.common.constant.CommonConstant;
import org.springblade.core.mp.support.BaseEntityWrapper;
import org.springblade.core.tool.node.ForestNodeMerger;
import org.springblade.core.tool.node.INode;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.SpringUtil;
import org.springblade.modules.system.entity.DictItem;
import org.springblade.modules.system.service.IDictItemService;
import org.springblade.modules.system.vo.DictItemVO;

import java.util.List;
import java.util.stream.Collectors;

public class DictItemWrapper extends BaseEntityWrapper<DictItem, DictItemVO> {

	private static IDictItemService dictItemService;

	static {
		dictItemService = SpringUtil.getBean(IDictItemService.class);
	}

	public static DictItemWrapper build() {
		return new DictItemWrapper();
	}

	@Override
	public DictItemVO entityVO(DictItem dictItem) {
		DictItemVO dictItemVO = BeanUtil.copy(dictItem, DictItemVO.class);
		if (Func.equals(dictItem.getParentId(), CommonConstant.TOP_PARENT_ID)) {
			dictItemVO.setParentName(CommonConstant.TOP_PARENT_NAME);
		} else {
			DictItem parent = dictItemService.getById(dictItem.getParentId());
			dictItemVO.setParentName(parent.getLabel());
		}
		return dictItemVO;
	}

	public List<INode> listNodeVO(List<DictItem> list) {
		List<INode> collect = list.stream().map(dict -> BeanUtil.copy(dict, DictItemVO.class)).collect(Collectors.toList());
		return ForestNodeMerger.merge(collect);
	}
}
